package activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.example.jaden.washeradministrator.*;
import com.example.jaden.washeradministrator.MyApplication;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import customControls.OrderConfirmedAdapter;
import order.Order;

public class OrderPage extends AppCompatActivity implements View.OnClickListener {

    private List<Order> orderList=new ArrayList<>();
    Button tobepaid;
    Button washing;
    Button  completed;
    ListView orderdisplay;
    Button tobecomfirmed;
    MyApplication application;
    OrderConfirmedAdapter adapterconfirm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //确保键盘不会遮住要输入的文本框
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.orderpage);
        //将当前活动加入list中进行管理
        ActivityManagement.activityList.add(this);

        //获取Toolbar的实例
        Toolbar toolbar=(Toolbar)findViewById(R.id.homepage_toolbar);
        setSupportActionBar(toolbar);

        //获取控件实例
        getInstance();

        //执行按键点击监听
        setClickListener();

        //initOrder();

        getOrder(1);
    }
    //获取控件实例
    private void getInstance() {

        tobepaid =(Button)findViewById(R.id.button_tobepaid_orderpage);
        washing =(Button)findViewById(R.id.button_washing_orderpage);
        completed=(Button)findViewById(R.id.button_completed_orderpage);
        tobecomfirmed=(Button)findViewById(R.id.button_tobeconfirmed_orderpage);
        orderdisplay = (ListView) findViewById(R.id.ListView_orderdisplay_orderpage);
        application=MyApplication.getInstance();
    }
    //执行按键点击监听
    private void setClickListener(){
        tobepaid.setOnClickListener(this);
        washing.setOnClickListener(this);
        completed.setOnClickListener(this);
        tobecomfirmed.setOnClickListener(this);
    }

    //初始化废品
    private void initOrder() {
        Order order = new Order();

        orderList.add(order);

    }

    private void getOrder(int orderstatus)
    {

        String url="http://47.96.159.236:8080/Wash/Order?method=getOrder";
        try {
            //打包Json数据
            JSONObject userJson=new JSONObject();
            userJson.put("OrderStatus",orderstatus);
            String messageJson=String.valueOf(userJson);
            TransferData.SelectOrder(url,messageJson);
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        while(true)
        {
            if(application.isSelectOrderOK())
            {
                application.setSelectOrderOK(false);
                //verification(application.getResponseMessage());
                orderList=application.getOrderList();
                //boolean x=orderList.isEmpty();
                if(orderList==null||orderList.isEmpty()) {
                    Toast.makeText(this, "无订单", Toast.LENGTH_LONG).show();
                    adapterconfirm = new OrderConfirmedAdapter(OrderPage.this, R.layout.order_listview_item_comfirmed, new ArrayList<Order>());
                    orderdisplay.setAdapter(adapterconfirm);
                }
                else {
                        adapterconfirm = new OrderConfirmedAdapter(OrderPage.this, R.layout.order_listview_item_comfirmed, orderList);
                        orderdisplay.setAdapter(adapterconfirm);
                }
                break;
            }
        }

    }

    public void verification(ResponseMessage responseMessage)
    {
        switch (responseMessage.getCode()) {
            case 4000:
                //Toast.makeText(this, responseMessage.getMsg(), Toast.LENGTH_LONG).show();
                break;
            case 4001:
                Toast.makeText(this, responseMessage.getMsg(), Toast.LENGTH_LONG).show();
                break;
        }
    }
    //按back键 销毁活动
    protected void onDestroy() {
        ActivityManagement.KillOneActivity();
        super.onDestroy();
    }
    //修改button的颜色，按下时显示绿色
    private void setButtonEnabled(boolean bt1,boolean bt2,boolean bt3,boolean bt4)
    {
        tobecomfirmed.setEnabled(bt1);
        tobepaid.setEnabled(bt2);
        washing.setEnabled(bt3);
        completed.setEnabled(bt4);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //查询待确认的订单
            case R.id.button_tobeconfirmed_orderpage:
                setButtonEnabled(false,true,true,true);
                getOrder(1);
                break;
                //查询待付款的订单
            case R.id.button_tobepaid_orderpage:
                setButtonEnabled(true,false,true,true);
                getOrder(2);

                break;
            //查询正在洗的订单
            case R.id.button_washing_orderpage:
                setButtonEnabled(true,true,false,true);
                getOrder(3);
                break;
                //查询已完成的订单
            case R.id.button_completed_orderpage:
                setButtonEnabled(true,true,true,false);
                getOrder(4);
                break;
            default:
                break;
        }
    }

}
